import * as Cesium from 'cesium';
import { VertexAttributeSemanticGaussian } from './fixVertexAttributeSemantic';
//@ts-ignore
const { defined, AttributeType } = Cesium;
export default function fixModelUtility() {
    //@ts-ignore
    const old = Cesium.ModelUtility.supportedExtensions;
    //@ts-ignore
    Cesium.ModelUtility.supportedExtensions = {
        ...old,
        KHR_gaussian_splatting: true,
    }
    //@ts-ignore
    Cesium.ModelUtility.getAttributeInfo = function (attribute) {
        const semantic = attribute.semantic;
        const setIndex = attribute.setIndex;

        let variableName;
        let hasSemantic = false;
        if (defined(semantic)) {
            variableName = VertexAttributeSemanticGaussian.getVariableName(semantic, setIndex);
            hasSemantic = true;
        } else {
            variableName = attribute.name;
            // According to the glTF 2.0 spec, custom attributes must be prepended with
            // an underscore.
            variableName = variableName.replace(/^_/, "");
            variableName = variableName.toLowerCase();
        }

        const isVertexColor = /^color_\d+$/.test(variableName);
        const attributeType = attribute.type;
        let glslType = AttributeType.getGlslType(attributeType);

        // color_n can be either a vec3 or a vec4. But in GLSL we can always use
        // attribute vec4 since GLSL promotes vec3 attribute data to vec4 with
        // the .a channel set to 1.0.
        if (isVertexColor) {
            glslType = "vec4";
        }

        const isQuantized = defined(attribute.quantization);
        let quantizedGlslType;
        if (isQuantized) {
            // The quantized color_n attribute also is promoted to a vec4 in the shader
            quantizedGlslType = isVertexColor
                ? "vec4"
                : AttributeType.getGlslType(attribute.quantization.type);
        }

        return {
            attribute: attribute,
            isQuantized: isQuantized,
            variableName: variableName,
            hasSemantic: hasSemantic,
            glslType: glslType,
            quantizedGlslType: quantizedGlslType,
        };
    };
}